﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
public class SceneMgr : Singleton<SceneMgr>
{
    public void LoadScene(string name,UnityAction func)
    {
        SceneManager.LoadScene(name);
        func();
    }

    public void LoadSceneAsync(string name,UnityAction func)
    {
        MonoMgr.Instance.StartCoroutine(ReallyLoadSceneAsync(name, func));
    }

    private IEnumerator ReallyLoadSceneAsync(string name,UnityAction func)
    {
        AsyncOperation ao = SceneManager.LoadSceneAsync(name);
        while(!ao.isDone)
        {
            EventCenter.Instance.EventTrigger("Loading", ao.progress);
            yield return ao.progress;
        }
        func();
    }

    public string GetActiveSceneName()
    {
        return SceneManager.GetActiveScene().name;
    }

    public void AALoadSceneAsync(string name, UnityAction func)
    {

        MonoMgr.Instance.StartCoroutine(AAReallyLoadSceneAsync(name + ".unity", func));
    }

    private IEnumerator AAReallyLoadSceneAsync(string name, UnityAction func)
    {
        AsyncOperationHandle async = Addressables.LoadSceneAsync(name);
        while (!async.IsDone)
        {
            EventCenter.Instance.EventTrigger("Loading", async.PercentComplete);
            yield return async.PercentComplete;
        }
        func();
    }
}
